package cn.vin.utils;

import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: Wu Shenzhuang
 * @Date: 2019/5/27 18:37
 */
public class ExcelPOIUtil {
    public void test() {
//        List<Map<String, Object>> maps = qqsysActionCustomDao.statisLevelTime(TableSuffixUtil.getTableSuffix(LocalDate.now().minusDays(3)));
        List<Map<String, Object>> maps = Lists.newArrayList();
        String filePath = "test.xlsx";
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet qqsys = wb.createSheet("球球刷一刷");
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);

        XSSFRow tt = qqsys.createRow(0);
        XSSFCell cell = tt.createCell(0);
        cell.setCellValue("关卡时间统计");
        CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
        qqsys.addMergedRegion(region);
        RegionUtil.setBorderBottom(BorderStyle.THIN,region,qqsys);
        RegionUtil.setBorderLeft(BorderStyle.THIN,region,qqsys);
        RegionUtil.setBorderRight(BorderStyle.THIN,region,qqsys);
        RegionUtil.setBorderTop(BorderStyle.THIN,region,qqsys);

        XSSFRow title = qqsys.createRow(1);
        XSSFCell c0 = title.createCell(0);
        c0.setCellStyle(cellStyle);
        c0.setCellValue("关卡");
        XSSFCell c1 = title.createCell(1);
        c1.setCellStyle(cellStyle);
        c1.setCellValue("总计");
        XSSFCell c2 = title.createCell(2);
        c2.setCellStyle(cellStyle);
        c2.setCellValue("均时（s）");
        for(int i=0;i<maps.size();i++) {
            Map<String, Object> map = maps.get(i);
            XSSFRow row = qqsys.createRow(i + 2);
            c0 = row.createCell(0);
            c1 = row.createCell(1);
            c2 = row.createCell(2);
            c0.setCellValue((int)map.get("extra_mark"));
            c0.setCellStyle(cellStyle);
            c1.setCellValue((long)map.get("level_count"));
            c1.setCellStyle(cellStyle);
            c2.setCellValue((double)map.get("level_time"));
            c2.setCellStyle(cellStyle);

        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filePath);
            wb.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("完成");
    }
}
